public class Solution962 {
    public int maxWidthRamp(int[] nums) {
        int[] f=new int[nums.length+1];
        f[0]=Integer.MAX_VALUE;
        int t=0;
        int ans=0;
        for (int i=0;i<nums.length;i++){
            if (t==0 || nums[i]<nums[f[t]]){
                t++;
                f[t]=i;
            }else{
                int l=1,r=t,a=-1;
                while (l<=r){
                    int mid=(l+r)/2;
                    if (nums[i]>=nums[f[mid]]){
                        a=f[mid];
                        r=mid-1;
                    }else{
                        l=mid+1;
                    }
                }
                ans=Math.max(ans,i-a);
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution962().maxWidthRamp(new int[]{6,0,8,2,1,5}));
    }
}
